package ar.edu.unq.tpi.uis.tepe3.wicket.pages;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;

import ar.edu.unq.tpi.uis.tepe3.dominio.Folder;
import ar.edu.unq.tpi.uis.tepe3.dominio.Grantable;
import ar.edu.unq.tpi.uis.tepe3.dominio.Group;
import ar.edu.unq.tpi.uis.tepe3.dominio.PrivType;
import ar.edu.unq.tpi.uis.tepe3.dominio.PrivTypeR;
import ar.edu.unq.tpi.uis.tepe3.dominio.PrivTypeRW;
import ar.edu.unq.tpi.uis.tepe3.dominio.Privilege;
import ar.edu.unq.tpi.uis.tepe3.dominio.User;
import ar.edu.unq.tpi.uis.tepe3.persistencia.GroupDAO;
import ar.edu.unq.tpi.uis.tepe3.persistencia.PrivilegeDAO;
import ar.edu.unq.tpi.uis.tepe3.persistencia.UserDAO;

public class PrivilegePage extends LayoutPage {
	
	private Privilege priv;
	private Folder folder;
	private Grantable grantable;
	private User user;
	private Group group;
	
	private PrivType type;
	
	public PrivilegePage(Folder folder){
		super();
		this.priv = new Privilege();
		this.folder = folder;
		this.initComponents();
	}

	protected void initComponents() {
		FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
		add(feedbackPanel);
		Form form = new Form("privForm", new CompoundPropertyModel(this.priv));
		this.addFields(form);
		this.addButtons(form);
		this.add(form);
	}
	
	private void chooseGrantee(){
		if (this.user == null && this.group == null){
			throw new RuntimeException("Debe elegir o un usuario o a un grupo para asignarle un permiso");
		}else if (this.user != null && this.group != null) {
			throw new RuntimeException("Debe elegir solo un usuario o ZOLO a un grupo para asignarle un permiso");
		}else if (this.user == null) {
			this.grantable = this.group;
		}else {
			this.grantable = this.user;
		}
	}

	protected void addButtons(Form form) {
		form.add(new Button("enviar") { 
			private static final long serialVersionUID = 1L;

			@Override 
            public void onSubmit() 
            { 
				try{
					chooseGrantee();
					savePrivilege();
				}catch(RuntimeException e){
				//	info("error en la creacion");
					throw e;
					//return;
				}
				this.setResponsePage(FolderPage.class);
            } 
        }); 
        form.add(new Link("cancel") {
			@Override
			public void onClick() {
				this.setResponsePage(FolderPage.class);
			}
		});

	}

	protected void addFields(Form form) {
		form.add(new Label("name", new PropertyModel(this.folder, "routeString")));
		form.add(new DropDownChoice("type", new PropertyModel(this, "type"), this.getAllPrivTypes()).setRequired(true));
		form.add(new DropDownChoice("users", new PropertyModel(this, "user"), this.getAllUsers()));
		form.add(new DropDownChoice("groups", new PropertyModel(this, "group"), this.getAllGroups()));
	}
	
	protected void savePrivilege(){
		PrivilegeDAO dao = new PrivilegeDAO();
		this.priv.setFolder(this.folder);
		this.priv.setType(this.type);
		this.priv.setGrantable(this.grantable);
		dao.makePersistent(this.priv);
		dao.flush();
	}

	private List<PrivType> getAllPrivTypes() {
		List<PrivType> list;// = new ArrayList<PrivType>();

		PrivilegeDAO dao = new PrivilegeDAO();
		list = dao.getPrivTypesInstances();
		//dao.flush();
		//list.add(new PrivTypeR());
		//list.add(new PrivTypeRW());
		return list;
	}
	
	private List<User> getAllUsers() {
		UserDAO dao = new UserDAO();
		List<User> list = dao.findAll();
		dao.flush();
		return list;
	}
	
	private List<Group> getAllGroups() {
		GroupDAO dao = new GroupDAO();
		List<Group> list = dao.findAll();
		dao.flush();
		return list;
	}
}
